﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CRUD
{
    public class DS_csv : IDS
    {
        public override void Create(Person a)
        {
            using (StreamWriter sw = File.AppendText(path))
            {              
                sw.WriteLine(a.SaveToSCV());                
                sw.Close();
            }
        }
        public override IDS IsReady(string Fname)
        {
            
            path = Fname;
            IDS ret = new DS_csv();
            string[] ext = path.Split('.','*');
            if (ext[1] == "csv")
            {
                ret = this;
            }
            else
            {
                if (!(next == null)) ret = next.IsReady(Fname);
            }
            return ret;
        }
        public override MyList Read()
        {
            MyList a = new MyList();
            StreamReader sr = new StreamReader(path);
            string str;
            while ((str = sr.ReadLine()) != null)
            {
                string[] split = str.Split(new Char[] { ';' });
                Person type = new Person();
                type.LoadFromCSV(split);
                a.Add(type);
            }
            sr.Close();
            return a;
        }
        public override void Update(Person pp) 
        {

            MyList a = new MyList();
            StreamReader sr = new StreamReader(path);
            StreamWriter sw = new StreamWriter(@"c:\temp\temp.csv");
            string str;
            while ((str = sr.ReadLine()) != null)
            {
                string[] split = str.Split(new Char[] { ';' });
                if (Convert.ToInt32(split[1]) != pp.id)
                {
                    sw.WriteLine(str);
                }
                else
                {
                    sw.WriteLine(pp.SaveToSCV());
                }
            }
            sw.Close();
            sr.Close();
           replace(path, @"c:\temp\temp.csv");
           
        }  
        public override void Delete(int id)
        {
        
            MyList a = new MyList();
            using (StreamReader sr = File.OpenText(path))
            {
                using (StreamWriter sw = File.CreateText(@"temp.csv"))
                {
                string str;
                while ((str = sr.ReadLine()) != null)
                {
                    string[] split = str.Split(new Char[] { ';' });                   
                    if (Convert.ToInt32(split[1]) != id)
                    {
                        sw.WriteLine(str);
                    }

                }
                sw.Close();
                }
                sr.Close();               
            }
            
            replace(path, @"temp.csv");
        }
        void replace(string destination,string source) 
        {
            using (StreamReader sr = File.OpenText(source))
            {
                using (StreamWriter sw = File.CreateText(destination))
                {
                    string str;
                    while ((str = sr.ReadLine()) != null)
                    {                       
                            sw.WriteLine(str);                       

                    }
                    sw.Close();
                }
                sr.Close();
            }
        }
    }

}
